package io.navigation.modules.navigation.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.navigation.modules.navigation.entity.NavigationClassificationEntity;
import io.navigation.modules.navigation.service.NavigationClassificationService;
import io.navigation.common.utils.PageUtils;
import io.navigation.common.utils.R;

/**
 * 导航分类表
 *
 * @author sunzhijun
 * @email ,sunzhijunadmin@163.com
 * @date 2019-01-23 20:42:42
 */
@RestController
@RequestMapping("api/navigation/navigationclassification")
@Api(tags = "导航分类表控制类")
public class NavigationClassificationController {
    @Autowired
    private NavigationClassificationService navigationClassificationService;

    /**
     * 列表
     */
    @GetMapping("/page")
//    @RequiresPermissions("navigation:navigationclassification:list")
    @ApiOperation("导航分类表列表")
    public R page(@RequestParam Map<String, Object> params){
        PageUtils page = navigationClassificationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据条件获取分类列表集合
     */
    @GetMapping("/queryList")
    @ApiOperation("根据条件获取分类集合")
    public R queryList(@RequestParam Map<String, Object> params){
        params.remove("params");
        List<NavigationClassificationEntity> list = navigationClassificationService.selectByMap(params);

        return R.ok().put("list", list);
    }

    /**
     * 获取所有分类的json
     */
    @GetMapping("/allJson")
    @ApiOperation("获取所有分类的json")
    public R allJson(){
        Map<String, Object> map = new HashMap<>();
        map.put("is_reveal", 1);
        map.put("is_private", 0);
        //获取所有可展示分类
        List<NavigationClassificationEntity> classList = this.navigationClassificationService.selectByMap(map);
        //通过递归的方式返回json拼串
        JSONArray jsonArray = queryJson(classList, "0");

        return R.ok().put("data", jsonArray);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("navigation:navigationclassification:info")
    @ApiOperation("导航分类表信息")
    public R info(@PathVariable("id") String id){
			NavigationClassificationEntity navigationClassification = navigationClassificationService.selectById(id);

        return R.ok().put("navigationClassification", navigationClassification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("navigation:navigationclassification:save")
    @ApiOperation("导航分类表保存")
    public R save(@RequestBody NavigationClassificationEntity navigationClassification){
			navigationClassificationService.insert(navigationClassification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresPermissions("navigation:navigationclassification:update")
    @ApiOperation("导航分类表修改")
    public R update(@RequestBody NavigationClassificationEntity navigationClassification){
			navigationClassificationService.updateById(navigationClassification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @RequiresPermissions("navigation:navigationclassification:delete")
    @ApiOperation("导航分类表删除")
    public R delete(@RequestBody String[] ids){
			navigationClassificationService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过递归的方式返回json拼串
     * @param classList 分类集合
     * @param parentId 父级id
     * @return
     */
    public JSONArray queryJson(List<NavigationClassificationEntity> classList, String parentId) {
        JSONArray jsonArray = new JSONArray();
        if (null != classList && 0 < classList.size()) {
            //遍历分类信息
            for (NavigationClassificationEntity nc : classList) {
                //获取分类的父级id为parentId的进行拼串
                if (nc.getParentId().equals(parentId)) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("id", nc.getId());
                    jsonObject.put("icon", nc.getClassIcon());
                    jsonObject.put("parentId", nc.getParentId());
                    jsonObject.put("name", nc.getName());
                    jsonObject.put("sort", nc.getSort());
                    jsonObject.put("description", nc.getDescription());
                    //获取下级分类进行写入
                    jsonObject.put("subsetJson", queryJson(classList, nc.getId()));
                    //数据写入集合
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }

}
